常見問題集


console.log 沒有輸出到 Linux 終端機

應在命令列中使用 --enable-logging=stderr;在此處查看更多資訊:https://www.chromium.org/for-testers/enable-logging

var crypto = require('crypto') 取得錯誤的物件

Chromium 有自己的全域 crypto 物件,無法覆寫。因此您無法使用相同的變數名稱 crypto。將變數名稱變更為其他名稱,例如 nodeCrypto,即可運作。

AngularJS 中的圖片損毀,且在 DevTools 中收到 無法載入資源 XXX net::ERR_UNKNOWN_URL_SCHEME

AngularJS 為未知的 scheme 新增 unsafe: 前置詞,以防止 XSS 攻擊。NW.js 和 Chrome 應用程式的 URL 以 chrome-extension: scheme 開頭,AngularJS 未知此 scheme。解決方案是透過加入下列各行,以 AngularJS 設定已知 scheme 的白名單

myApp.config(['$compileProvider',
  function($compileProvider) {
    $compileProvider.imgSrcSanitizationWhitelist(/^\s*((https?|ftp|file|blob|chrome-extension):|data:image\/)/);
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|tel|file|chrome-extension):/);
  }]);

在 AngularJS 2+ 中看不到例外報告

AngularJS 2 嘗試在全域變數 global 中註冊例外處理常式。不過,它已存在於 NW.js 環境中,這會阻止在 DevTools 中顯示例外報告。解決方法是在載入任何 AngularJS 函式庫之前,將 global 重新命名為其他名稱。例如,

<script>
window.nw_global = window.global;
window.global = undefined;
</script>
<!-- Angular 2 Dependencies -->

如何使用 ESC 鍵離開全螢幕模式?

使用者通常預期使用 ESC 鍵退出全螢幕模式。預設情況下,NW.js 沒有繫結 ESC 快速鍵來離開全螢幕模式,但提供輸入和離開全螢幕模式的 API。這將讓開發人員更能控制全螢幕模式。

若要啟用 ESC 鍵離開全螢幕模式,您可以使用 快速鍵 API

nw.App.registerGlobalHotKey(new nw.Shortcut({
  key: "Escape",
  active: function () {
    // decide whether to leave fullscreen mode
    // then ...
    nw.Window.get().leaveFullscreen();
  }
}));

如何在 VSCode 中為 nw 取得「intellisense」(自動完成)?

Screenshot of VSCode Intellisense for NW.js

  1. 安裝 nwjs-types
    npm install --save-dev nwjs-types
    
  2. 強制載入 VSCode 的 TypeScript 引擎。
  3. // @ts-check 放置在 JS 檔案的最上方。
  4. 您不需要使用 TypeScript 即可執行此操作。
  5. 當 VSCode 看見 // @ts-check 時,它將載入 TypeScript 引擎,然後在您的 node_modules 中尋找類型,接著尋找並載入 nwjs-types
  6. 然後,Intellisense 將知道如何為 nw. 自動完成。